package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.bean.Book;
import org.example.bean.Category;
import org.example.handler.CategoryRemoveExveption;
import org.example.mapper.BookMapper;
import org.example.mapper.CateGoryMapper;
import org.example.service.CateGoryService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * @Auther:Liu
 * @Testname:CateGoryServiceImpl
 * @Date:2025/8/7 8:35
 */
@Service
public class CateGoryServiceImpl extends ServiceImpl<CateGoryMapper, Category> implements CateGoryService {

    @Resource
    private BookMapper bookMapper;


    @Override
    public JsonResult findAllCateGory() {

        return ResultTool.success(list());

    }

    @Override
    public JsonResult removeByCateId1(String categoryId) {
        boolean b1 = removeById(categoryId);
        if (!b1){
            throw new CategoryRemoveExveption("删除分类失败");
        }
        return ResultTool.success("success");
    }

    @Transactional(rollbackFor = {SQLException.class ,CategoryRemoveExveption.class})
    public JsonResult removeByCateId2(String categoryId) {
        boolean b1 = removeById(categoryId);
        if (!b1){
            throw new CategoryRemoveExveption("删除分类失败");
        }
        UpdateWrapper<Book> wrapper = new UpdateWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.set("category_id",null);
        int i = bookMapper.update(wrapper);
        if (i  ==0){
            throw new CategoryRemoveExveption("删除分类失败");
        }
        return ResultTool.success("success");
    }
}
